<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
    <title>我的账户</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        body {
            font-family: Arial, sans-serif;
            background-color: #f8f9fa;
            color: #333;
            line-height: 1.6;
        }
        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 0 15px;
        }
        .profile-container {
            max-width: 800px;
            margin: 50px auto;
            padding: 30px;
            background-color: #fff;
            border-radius: 10px;
            box-shadow: 0 0 20px rgba(0,0,0,0.1);
        }
        .profile-header {
            text-align: center;
            margin-bottom: 30px;
        }
        .profile-header h2 {
            color: #333;
            font-size: 24px;
            margin-bottom: 10px;
        }
        .avatar-container {
            width: 150px;
            height: 150px;
            margin: 0 auto 20px;
            border-radius: 50%;
            overflow: hidden;
            border: 3px solid #fff;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
        }
        .avatar-container img {
            width: 100%;
            height: 100%;
            object-fit: cover;
        }
        .form-group {
            margin-bottom: 20px;
        }
        .form-label {
            display: block;
            margin-bottom: 8px;
            font-weight: 500;
            color: #333;
        }
        .form-control {
            width: 100%;
            padding: 10px 15px;
            font-size: 16px;
            border: 1px solid #ddd;
            border-radius: 4px;
            transition: border-color 0.3s;
        }
        .form-control:focus {
            border-color: #007bff;
            outline: none;
        }
        .btn {
            display: inline-block;
            padding: 10px 20px;
            font-size: 16px;
            font-weight: 500;
            text-align: center;
            text-decoration: none;
            border-radius: 4px;
            cursor: pointer;
            transition: background-color 0.3s;
        }
        .btn-primary {
            background-color: #007bff;
            color: #fff;
            border: none;
        }
        .btn-primary:hover {
            background-color: #0056b3;
        }
        .text-center {
            text-align: center;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="profile-container">
            <div class="profile-header">
                <h2>我的账户</h2>
            </div>
            
            <form id="updateForm" enctype="multipart/form-data">
                <div class="avatar-container">
                    <img src="${user.avatar}" alt="用户头像" id="avatarPreview">
                </div>
                
                <div class="form-group">
                    <label for="avatar" class="form-label">更换头像</label>
                    <input type="file" class="form-control" id="avatar" name="avatar" accept="image/*">
                </div>
                
                <div class="form-group">
                    <label for="account" class="form-label">账号</label>
                    <input type="text" class="form-control" id="account" name="account" value="${user.account}">
                </div>
                
                <div class="form-group">
                    <label for="telphone" class="form-label">手机号码</label>
                    <input type="tel" class="form-control" id="telphone" name="telphone" value="${user.telphone}">
                </div>
                
                <div class="form-group">
                    <label for="email" class="form-label">电子邮箱</label>
                    <input type="email" class="form-control" id="email" name="email" value="${user.email}">
                </div>
                
                <div class="form-group">
                    <label for="password" class="form-label">新密码</label>
                    <input type="password" class="form-control" id="password" name="password" placeholder="不修改请留空">
                </div>
                
                <div class="form-group">
                    <label for="repassword" class="form-label">确认新密码</label>
                    <input type="password" class="form-control" id="repassword" name="repassword" placeholder="不修改请留空">
                </div>
                
                <div class="text-center">
                    <button type="submit" class="btn btn-primary">保存修改</button>
                </div>
            </form>
        </div>
    </div>

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function() {
            // 预览头像
            $('#avatar').change(function() {
                if (this.files && this.files[0]) {
                    var reader = new FileReader();
                    reader.onload = function(e) {
                        $('#avatarPreview').attr('src', e.target.result);
                    }
                    reader.readAsDataURL(this.files[0]);
                }
            });
            
            // 提交表单
            $('#updateForm').submit(function(e) {
                e.preventDefault();
                
                // 验证密码
                var password = $('#password').val();
                var repassword = $('#repassword').val();
                if (password !== repassword) {
                    alert('两次输入的密码不一致！');
                    return;
                }
                
                var formData = new FormData(this);
                
                $.ajax({
                    url: '/user/updateUserInfo',
                    type: 'POST',
                    data: formData,
                    processData: false,
                    contentType: false,
                    success: function(response) {
                        if (response === 'success') {
                            alert('更新成功！');
                            if (password) {
                                // 如果修改了密码，跳转到登录页面
                                window.location.href = '/user/login';
                            } else {
                                // 头像或其他信息更新，跳转到首页并刷新
                                window.location.href = '/';
                            }
                        } else {
                            alert(response);
                        }
                    },
                    error: function() {
                        alert('系统错误，请稍后再试');
                    }
                });
            });
        });
    </script>
</body>
</html> 